package com.zhongyewx.teachercert.utils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.google.gson.Gson;
import com.zhongyewx.teachercert.ZYApplication;

import java.io.Serializable;
import java.util.Set;

public class ZYSharedPreferencesUtils {

    private static final String TAG = "ZYSharedPreferencesUtils";
    private static SharedPreferences mSp;
    private static final String preferences_name = "ZYTeacherCertShared";
    public static void initPreferences(Context context) {
        try {
            if (mSp != null) {
                return;
            }
            synchronized (ZYSharedPreferencesUtils.class) {
                mSp = context.getSharedPreferences(preferences_name, Activity.MODE_PRIVATE);
            }
        } catch (Exception e) {
            ZYLog.trace(TAG,
                    "init preferences xml file exception:" + ZYLog.getExceptionStactTrace(e));
        }
    }
    private static void initInstance(){
        if (mSp == null) {
            initPreferences(ZYApplication.getInstance().getApplication());
        }
    }

    public static String getStringValue(String key){
        initInstance();
        try {
            return mSp.getString(key, "");
        } catch (Exception e) {
            return "";
        }
    }

    public static int getIntValue(String key){
        initInstance();
        try {
            return mSp.getInt(key, -1);
        } catch (Exception e) {
            return -1;
        }
    }
    public static long getLongValue(String key){
        initInstance();
        try {
            return mSp.getLong(key, -1);
        } catch (Exception e) {
            return -1;
        }
    }
    public static float getFloatValue(String key){
        initInstance();
        try {
            return mSp.getFloat(key, 0.0f);
        } catch (Exception e) {
            return 0.0f;
        }
    }
    public static boolean getBooleanValue(String key){
        initInstance();
        try {
            return mSp.getBoolean(key, false);
        } catch (Exception e) {
            return false;
        }
    }

    public static <T extends Serializable> T getObjectValue(String key, T o){
        initInstance();
        return (T) new Gson().fromJson(mSp.getString(key, null),o.getClass());
    }


    public static void putBooleanValue(String key, boolean b){
        initInstance();
        if (mSp == null) {
            return;
        }
        apply(mSp.edit().putBoolean(key, b));
    }

    public static void putStringValue(String key, @Nullable String b){
        initInstance();
        if (mSp == null) {
            return;
        }
        apply(mSp.edit().putString(key, b));
    }

    public static void putStringSetValue(String key, @Nullable Set<String> b){
        initInstance();
        if (mSp == null) {
            return;
        }
        apply(mSp.edit().putStringSet(key, b));
    }

    public static void putIntValue(String key, int b){
        initInstance();
        if (mSp == null) {
            return;
        }
        apply(mSp.edit().putInt(key, b));
    }

    public static void putLongValue(String key, long b){
        initInstance();
        if (mSp == null) {
            return;
        }
        apply(mSp.edit().putLong(key, b));
    }

    public static void putFloatValue(String key, float b){
        initInstance();
        if (mSp == null) {
            return;
        }
        apply(mSp.edit().putFloat(key, b));
    }

    public static <T extends Serializable> void putObjectValue(String key, T o) {
        initInstance();
        if (mSp == null) {
            return;
        }
        apply(mSp.edit().putString(key, new Gson().toJson(o)));
    }

    /**
     * 清除所有数据
     */
    public static void clear() {
        initInstance();
        commit(mSp.edit().clear());
    }

    public static void delete(String name) {
        initInstance();
        try {
            commit(mSp.edit().remove(name));
        } catch (Exception e) {
//            ZYLog.trace(
//                    TAG,
//                    "delete key-value from preferences xml and key:"
//                            + ZYLog.getExceptionStactTrace(e));
        }
    }

    private static void apply(@NonNull SharedPreferences.Editor editor) {
        if (editor == null) {
            return;
        }
        try {
            editor.apply();
        } catch (AbstractMethodError var3) {
            commit(editor);
        }
    }

    private static void commit(@NonNull SharedPreferences.Editor editor) {
        if (editor == null) {
            return;
        }
        editor.commit();
    }
}
